package com.synface.utils.Logs;

import android.util.Log;

public class Logs
{
  private static boolean sIsLogEnabled = true;

  private static String sApplicationTag = "Logs";
  private static final String TAG_CONTENT_PRINT = "%s:%s.%s:%d";

  public static void releaseLog(String logTag, String log)
  {
    Log.e(logTag, log);
  }

  public static void debugLog(String logTag, String log) {
    if (sIsLogEnabled)
      Log.e(logTag, log);
  }

  private static StackTraceElement getCurrentStackTraceElement()
  {
    return Thread.currentThread().getStackTrace()[4];
  }

  public static void trace()
  {
    if (sIsLogEnabled)
      Log.d(sApplicationTag, getContent(getCurrentStackTraceElement()));
  }

  private static String getContent(StackTraceElement trace)
  {
    return String.format("%s:%s.%s:%d", new Object[] { sApplicationTag, trace
      .getClassName(), trace.getMethodName(), 
      Integer.valueOf(trace
      .getLineNumber()) });
  }

  private static String getContents(StackTraceElement trace)
  {
    return String.format("%s:%s:%d", new Object[] { sApplicationTag, trace
      .getMethodName(), Integer.valueOf(trace.getLineNumber()) });
  }

  public static void traceStack()
  {
    if (sIsLogEnabled)
      traceStack(sApplicationTag, 6);
  }

  public static void traceStack(String tag, int priority)
  {
    if (!sIsLogEnabled)
      return;
    StackTraceElement[] stackTrace = Thread.currentThread()
      .getStackTrace();
    Log.println(priority, tag, stackTrace[4].toString());
    StringBuilder str = new StringBuilder();
    String prevClass = null;
    for (int i = 5; i < stackTrace.length; ++i) {
      String className = stackTrace[i].getFileName();
      int idx = className.indexOf(".java");
      if (idx >= 0) {
        className = className.substring(0, idx);
      }
      if ((prevClass == null) || (!prevClass.equals(className)))
      {
        str.append(className.substring(0, idx));
      }

      prevClass = className;
      str.append(".").append(stackTrace[i].getMethodName())
        .append(":")
        .append(stackTrace[i].getLineNumber())
        .append("->");
    }

    Log.println(priority, tag, str.toString());
  }

  public static void v(String msg)
  {
    if (!sIsLogEnabled) {
      return;
    }

    Log.v(sApplicationTag, new StringBuilder().append(getContents(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void d(String tag, String msg)
  {
    if (sIsLogEnabled)
      Log.d(tag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void ds(String msg)
  {
    if (sIsLogEnabled)
      Log.d(sApplicationTag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void d(String msg)
  {
    if (!sIsLogEnabled) {
      return;
    }

    Log.d(sApplicationTag, new StringBuilder().append(getContents(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void i(String tag, String msg)
  {
    if (sIsLogEnabled)
      Log.i(tag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void d(String message, Object[] args)
  {
    if (sIsLogEnabled)
      d(String.format(message, args));
  }

  public static void w(String tag, String msg)
  {
    if (sIsLogEnabled)
      Log.w(tag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void e(String tag, String msg)
  {
    if (sIsLogEnabled)
      Log.e(tag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void i(String msg)
  {
    if (sIsLogEnabled)
      Log.i(sApplicationTag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void w(String msg)
  {
    if (sIsLogEnabled)
      Log.w(sApplicationTag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append(">").append(msg).toString());
  }

  public static void e(String msg)
  {
    if (sIsLogEnabled)
      Log.e(sApplicationTag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append("\n>").append(msg).toString());
  }

  public static void e(Exception exception)
  {
    if (sIsLogEnabled) {
      Log.e(sApplicationTag, new StringBuilder().append(getContent(getCurrentStackTraceElement())).append("\n>")
        .append(exception
        .getMessage()).toString());

      exception.printStackTrace();
    }
  }

  public static void e(Exception exception, String string) {
    if (sIsLogEnabled) {
      Log.e(sApplicationTag, new StringBuilder()
        .append(getContent(getCurrentStackTraceElement())).append("\n>")
        .append(exception
        .getMessage()).append("\n>")
        .append(exception
        .getStackTrace()).append("   ").append(string).toString());

      exception.printStackTrace();
    }
  }

  public static void e(String string, Exception exception) {
    if (sIsLogEnabled) {
      Log.e(sApplicationTag, new StringBuilder()
        .append(getContent(getCurrentStackTraceElement())).append("\n>")
        .append(exception
        .getMessage()).append("\n>")
        .append(exception
        .getStackTrace()).append("   ").append(string).toString());

      exception.printStackTrace();
    }
  }

  public static void e(String tag, String message, Exception exception) {
    if (sIsLogEnabled) {
      Log.e(tag, new StringBuilder()
        .append(getContent(getCurrentStackTraceElement())).append("\n>")
        .append(exception
        .getMessage()).append("\n>")
        .append(exception
        .getStackTrace()).append("   ").append(message).toString());

      exception.printStackTrace();
    }
  }

  public static boolean issIsLogEnabled() {
    return sIsLogEnabled;
  }

  public static void setsIsLogEnabled(boolean sIsLogEnabled) {
    sIsLogEnabled = sIsLogEnabled;
  }

  public static String getsApplicationTag() {
    return sApplicationTag;
  }

  public static void setsApplicationTag(String sApplicationTag) {
    sApplicationTag = sApplicationTag;
  }

  public static String getTagContentPrint() {
    return "%s:%s.%s:%d";
  }
}